package cn.howxu.yichen_ovo.common.item;


import cn.howxu.yichen_ovo.client.render.entity.DollEntityItemRender;
import cn.howxu.yichen_ovo.common.entity.DollEntity;
import cn.howxu.yichen_ovo.init.ModEntities;
import cn.howxu.yichen_ovo.init.ModItems;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.Mth;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.*;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.client.extensions.common.IClientItemExtensions;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.function.Consumer;


/**
 * @description: TODO
 * @author: HowXu
 * @date: 2025/9/12 18:25
 */
public class DollEntityItem extends Item {
    private static final String TAG_DOLL_ENTITY = "doll_entity";

    public DollEntityItem() {
        super(new Properties());
    }

    public static ItemStack createItemWithEntity(DollEntity entity) {
        ItemStack stack = new ItemStack(ModItems.DOLL_ENTITY_ITEM.get());
        saveDollEntity(stack, entity);
        return stack;
    }

    public static ItemStack createItemWithBlockState(BlockState state) {
        ItemStack stack = new ItemStack(ModItems.DOLL_ENTITY_ITEM.get());
        CompoundTag entityTag = new CompoundTag();
        entityTag.put("doll_state", NbtUtils.writeBlockState(state));
        CompoundTag stackTag = stack.getOrCreateTag();
        stackTag.put(TAG_DOLL_ENTITY, entityTag);
        return stack;
    }

    public static void saveDollEntity(ItemStack stack, DollEntity entity) {
        if (!stack.is(ModItems.DOLL_ENTITY_ITEM.get())) {
            return;
        }

        CompoundTag entityTag = new CompoundTag();
        entity.addAdditionalSaveData(entityTag);
        entity.removePhantomRecord(entityTag);

        CompoundTag stackTag = stack.getOrCreateTag();
        stackTag.put(TAG_DOLL_ENTITY, entityTag);
    }

    public static DollEntity getDollEntity(Level level, ItemStack stack) {
        if (!stack.is(ModItems.DOLL_ENTITY_ITEM.get())) {
            return new DollEntity(ModEntities.DOLL.get(), level);
        }

        CompoundTag stackTag = stack.getTag();
        if (stackTag != null && stackTag.contains(TAG_DOLL_ENTITY)) {
            CompoundTag entityTag = stackTag.getCompound(TAG_DOLL_ENTITY);
            DollEntity entity = new DollEntity(ModEntities.DOLL.get(), level);
            entity.load(entityTag);
            return entity;
        } else {
            return new DollEntity(ModEntities.DOLL.get(), level);
        }
    }
    

    @Override
    public void initializeClient(Consumer<IClientItemExtensions> consumer) {
        consumer.accept(new IClientItemExtensions() {
            private DollEntityItemRender render = null;

            @Override
            public BlockEntityWithoutLevelRenderer getCustomRenderer() {
                Minecraft minecraft = Minecraft.getInstance();
                if (render == null) {
                    render = new DollEntityItemRender(minecraft.getBlockEntityRenderDispatcher(), minecraft.getEntityModels());
                }
                return render;
            }
        });
    }

    private boolean mayPlace(Player pPlayer, Direction pDirection, ItemStack pItemStack, BlockPos pPos) {
        return !pPlayer.level().isOutsideBuildHeight(pPos) && pPlayer.mayUseItemAt(pPos, pDirection, pItemStack);
    }

    @Override
    public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
        ItemStack itemStack = player.getItemInHand(hand);
        return InteractionResultHolder.pass(itemStack);
    }
}

